package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.io.InputStream;
import java.util.Map;

public class ProjectNodeInfoList implements ServerInterface {

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            Map<String, String> map = JsonUtils.objectToPojo(req.getData(), Map.class);
            if (map == null || StringUtils.isEmpty(map.get("receiptType"))) {
                return MessageBean.create(Constant.NOT_NULL, "receiptType不能为空", null);
            }
            ClassLoader classLoader = ProjectNodeInfoList.class.getClassLoader();
            try (InputStream inputStream = classLoader.getResourceAsStream("template/json/" + userInfo.getCurWaterCode() + "/" + map.get("receiptType") + ".json");) {
                String data = IOUtils.toString(inputStream, "UTF-8");
                MessageBean message = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", void.class);
                message.setData(data);
                return message;
            }
        } catch (Exception e) {
            logger.error("获取流程节点信息失败:" + e, e);
        }
        return MessageBean.create(Constant.MESSAGE_INT_FAIL, "获取流程节点信息失败", null);
    }
}